package com.example.fresheveryday.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.fresheveryday.mapper.ProductMapper;
import com.example.fresheveryday.mapper.ShopcartMapper;
import com.example.fresheveryday.mapper.SuborderMapper;
import com.example.fresheveryday.po.Productorder;
import com.example.fresheveryday.mapper.ProductorderMapper;
import com.example.fresheveryday.po.Shopcart;
import com.example.fresheveryday.po.Suborder;
import com.example.fresheveryday.service.ProductService;
import com.example.fresheveryday.service.ProductorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ll
 * @since 2023-07-05
 */
@Service
public class ProductorderServiceImpl extends ServiceImpl<ProductorderMapper, Productorder> implements ProductorderService {
    @Autowired
    private ProductorderMapper productorderMapper;
    @Autowired
    private ShopcartMapper shopcartMapper;
    @Autowired
    private SuborderMapper suborderMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public String createOrder(Productorder productorder,int id[]) {
        //订单表新增
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        productorder.setCreated(new Date());
        productorder.setState(1);
        productorderMapper.insert(productorder);
        String orderId = productorder.getOrderid();

        //订单明细新增
        Suborder suborder = null;
        for (int i = 0; i < id.length;i++) {

            Shopcart shopcart = shopcartMapper.selectOne(new QueryWrapper<Shopcart>().eq("productId",id[i]));
            suborder = new Suborder();
            suborder.setProductid(id[i]);
            suborder.setOrderid(productorder.getOrderid());
            suborder.setQuantity(shopcart.getQuantity());
            suborder.setMoney(shopcart.getMoney());
            suborderMapper.insert(suborder);
            //增加销量和减少库存
            productMapper.addSaleCount(shopcart.getQuantity(),id[i]);
            productMapper.subCount(shopcart.getQuantity(),id[i]);
            shopcartMapper.delete(new QueryWrapper<Shopcart>().eq("productId",id[i]).eq("userId",productorder.getUserid()));//清除购物车
        }
        return orderId;

    }

    @Override
    public Productorder getOrdersByOrdersId(String ordersId) {
        return productorderMapper.getOrdersByOrdersId(ordersId);
    }

    @Override
    public List<Productorder> getOrdersList(String userId) {
        return productorderMapper.getOrdersList(userId);
    }

    @Override
    public IPage<Productorder> getOrderByConditions(IPage<Productorder> page, Wrapper wrapper) {
        return productorderMapper.getOrderByConditions(page,wrapper);
    }

    @Override
    public int countByConditions(Wrapper wrapper) {
        return productorderMapper.countByConditions(wrapper);
    }
}
